﻿using System;
using System.Collections.Generic;
using System.Text;
using Kitware.VTK;




namespace Fu3DEngine
{
   public  class Image2DNode:NodeBase 
    {

       public Image2DNode(string id)
           : base(id) 
       {
       }

       public override void Run()
       {
           base.Run();

           //this.CreateMain();
           //this.CreateContour();
           //this.CreateOutline();

           this.Test();


       }

       public void CreateContour()
       {

         


   

           vtkImageData img = DataSource.GetImageFromGridBinary();


           vtkImageDataGeometryFilter geo = vtkImageDataGeometryFilter.New();
           geo.SetInput(img);



           vtkContourFilter con = vtkContourFilter.New();
           con.SetInput(geo.GetOutput());
           con.GenerateValues(10, 0.0001, 0.01);



           //vtkBandedPolyDataContourFilter con = vtkBandedPolyDataContourFilter.New();
           //con.SetInput(geo.GetOutput());
           //con.GenerateValues(20, 0.0001, 0.01);
           //con.GenerateContourEdgesOn();


           double[] drange = img.GetScalarRange();

           vtkLogLookupTable lt = vtkLogLookupTable.New();
           lt.SetRange(drange[0], drange[1]);
           lt.Build();
          


           vtkPolyDataMapper pmapper = vtkPolyDataMapper.New();
           pmapper.SetInput(con.GetOutput());
           pmapper.SetLookupTable(lt);
           pmapper.SetScalarRange(drange[0], drange[1]);


           vtkActor actor = vtkActor.New();
           actor.SetMapper(pmapper );



           this.SParent.m_vtkRenderer.AddActor(actor);


           //vtkTexture tex = vtkTexture.New();
           //tex.SetInput(con.GetOutput());
           //tex.InterpolateOff();
           //tex.RepeatOff();


           //vtkPlaneSource p = vtkPlaneSource.New();

           //vtkPolyDataMapper2D m = vtkPolyDataMapper2D.New();
           //m.SetInput(p.GetOutput());



           //vtkTexturedActor2D a2 = vtkTexturedActor2D.New();
           //a2.SetTexture(tex);
           //a2.SetMapper(m);

           //this.SParent.m_vtkRenderer.AddActor(a2);


          

       }

       public void CreateOutline()
       {
           vtkImageData img2 = DataSource.GetImageFromGridBinary();


           vtkImageDataGeometryFilter geo2 = vtkImageDataGeometryFilter.New();
           geo2.SetInput(img2);

           vtkOutlineFilter outline = vtkOutlineFilter.New();
           outline.SetInput(geo2.GetOutput());


           vtkPolyDataMapper pmapper2 = vtkPolyDataMapper.New();
           pmapper2.SetInput(outline.GetOutput());

           vtkActor actor2 = vtkActor.New();
           actor2.SetMapper(pmapper2);
           actor2.GetProperty().SetColor(0, 0, 0);

           this.SParent.m_vtkRenderer.AddActor(actor2);
        
       }

      public void CreateMain()
      {
          vtkImageData img = DataSource.GetImageFromGridBinary();

          vtkImageDataGeometryFilter geo = vtkImageDataGeometryFilter.New();

          geo.SetInput(img);

         double[] drange= img.GetScalarRange();

          vtkLogLookupTable lt = vtkLogLookupTable.New();
          lt.SetRange(drange[0],drange [1]);
          lt.Build();
          

          vtkPolyDataMapper mapper = vtkPolyDataMapper.New();
          mapper.SetInput(geo.GetOutput());
          mapper.SetLookupTable(lt);
          mapper.SetScalarRange(drange[0],drange[1]);


          vtkActor actor = vtkActor.New();
          actor.SetMapper(mapper);

         

          this.SParent.m_vtkRenderer.AddActor2D(actor);











          vtkScalarBarWidget bar = vtkScalarBarWidget.New();
          bar.SetCurrentRenderer(this.SParent.m_vtkRenderer);
          bar.SetSelectable(1);
          bar.SetResizable(1);


          vtkScalarBarActor m_scalarBarActor = new vtkScalarBarActor();

          m_scalarBarActor.SetLookupTable(lt);



          m_scalarBarActor.SetOrientationToVertical();
          //scalarBarActor.SetPosition2(0.1, 0.1);
          m_scalarBarActor.GetPositionCoordinate().SetCoordinateSystemToNormalizedViewport();
          m_scalarBarActor.GetPositionCoordinate().SetValue(0.05, 0.2);

          m_scalarBarActor.SetWidth(0.12);
          m_scalarBarActor.SetHeight(0.8);
          m_scalarBarActor.SetTitle("height and color");

          m_scalarBarActor.SetPickable(1);
          m_scalarBarActor.DragableOn();

          this.SParent.m_vtkRenderer.AddActor(m_scalarBarActor);



          vtkScalarBarWidget bar2 = vtkScalarBarWidget.New();
          bar2.SetInteractor(this.SParent.m_vtkInteractor);
          bar2.SetScalarBarActor(m_scalarBarActor);

          bar2.GetScalarBarActor().SetTitle("noise");
          bar2.GetScalarBarActor().SetLookupTable(lt);



          bar2.SetSelectable(1);
          bar2.SetResizable(1);

          
          
         


      }

       public void Test()
       {
           vtkImageData img = DataSource.GetImageFromGridBinary();
        

           vtkImageDataGeometryFilter geo = vtkImageDataGeometryFilter.New();

           geo.SetInput(img);
           


           double[] drange = img.GetScalarRange();

           vtkLogLookupTable lt = vtkLogLookupTable.New();
           lt.SetRange(drange[0], drange[1]);
           lt.Build();


           vtkTexture tex = vtkTexture.New();
           tex.SetInput(img);
           tex.RepeatOff();
           tex.SetLookupTable(lt);
           tex.SetMapColorScalarsThroughLookupTable(1);
           tex.InterpolateOn();



           vtkPlaneSource plane = vtkPlaneSource.New();
           plane.SetXResolution(20);
           plane.SetYResolution(20);
           plane.SetOrigin(0,0,0);
           plane.SetPoint1(20000,0,0);
           plane.SetPoint2(0,20000,0);


           vtkPolyDataMapper mapper = vtkPolyDataMapper.New();
           mapper.SetInput(plane.GetOutput());
           mapper.SetLookupTable(lt);
           mapper.SetScalarRange(drange[0], drange[1]);
           
          


           vtkActor actor = vtkActor.New();

           actor.SetMapper(mapper);
           actor.SetTexture(tex);


           this.SParent.m_vtkRenderer.AddActor2D(actor);
         
       }

       public void CreateScalarWidget()
       {
           

           //vtkScalarBarWidget bar = vtkScalarBarWidget.New();
           //bar.SetInteractor(this.SParent .m_vtkRenderWindow .GetInteractor ());
           //bar.GetScalarBarActor().SetTitle("noise");
           //bar.GetScalarBarActor().SetLookupTable();
           //bar.SetSelectable(1);
           //bar.SetResizable(1);
       }

       public override void ReLoad()
       {
           throw new NotImplementedException();
       }

       public override void Unload()
       {
           base.Unload();

       }

    }
}
